package com.hujiang.basic.search.common.converter.req;

import com.hujiang.basic.search.common.converter.Converter;
import com.hujiang.basic.search.model.HJUpdateByQueryRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

@Component
public class UpdateByQueryRequestConverter implements Converter<HJUpdateByQueryRequest, UpdateByQueryRequest> {

    @Override
    public UpdateByQueryRequest convert(HJUpdateByQueryRequest in) {
        SearchRequest searchReq = new SearchRequest().indices(in.getIndex()).types(in.getType());
        SearchSourceBuilder ssb = new SearchSourceBuilder().query(in.getQuery().toJSONString());
        searchReq.source(ssb);
        UpdateByQueryRequest req = new UpdateByQueryRequest(searchReq);
        req.setScript(new Script(in.getScript()));
        req.setAbortOnVersionConflict(in.getAbortOnVersionConflict());
        return req;
    }
}
